package medium;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 826. 安排工作以达到最大收益
 * 创建时间：2024-05-17 17:18
 */
public class 安排工作以达到最大收益_826 {
    class Solution {
        public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
            int n = difficulty.length;
            int[][] job = new int[n][2];
            for (int i = 0; i < n; i++) {
                job[i][0] = difficulty[i];
                job[i][1] = profit[i];
            }

            Arrays.sort(worker);
            Arrays.sort(job, Comparator.comparingInt(a -> a[0]));
            int i = 0, ans = 0, max = 0;
            for (int w : worker) {
                while(i < n && job[i][0] <= w) {
                    max = Math.max(job[i++][1], max);
                }
                ans += max;
            }
            return ans;
        }
    }
}
